# -*- coding: utf-8 -*-
"""
 @Time    : 2021/1/26 下午6:01
 @Author  : tgy
 
"""
import itertools


def permutations(iterable, size=None):
    if size is None:
        size = len(iterable)

    # 如果size大于可迭代对象的长度，则直接返回空列表
    if size > len(iterable):
        return []
    add = []
    yield from find(iterable, add, size)


def find(iterable, add, size):
    ...
    if size == 0:
        yield tuple(add)
    else:
        for el in iterable:
            ...
            if el not in add:
                add.append(el)
                yield from find(iterable, add, size - 1)
                add.pop()


# 组合
def combinations(iterable):
    ...
    res = []
    yield from find_combin(iterable, 0, res)


def find_combin(iterable, index, res):
    ...
    if index == len(iterable):
        yield res
    else:
        yield from find_combin(iterable, index + 1, res)
        res.append(iterable[index])
        yield from find_combin(iterable, index + 1, res)
        res.pop()


def test01():
    # per = iter(permutations(range(1,10),3))
    # print(next(per))
    # print(next(per))
    # print(next(per))
    for val in permutations('abcdefg', 6):
        print(val)


if __name__ == '__main__':
    ...
    # print(permutations('abc', 3))
    # test01()
    # for val in combinations('abcdefg'):
    #     print(val)
    # for val in itertools.islice(combinations('abcdef'),10,10):
    #     print(val)
    for val in itertools.dropwhile(lambda x: len(x) == 0, combinations('abcdefg')):
        print(val)